{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "67cd69c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "时间区间\t\t黄金涨幅\t纳斯达克涨幅\n",
      "2022-04-04 ~ 2022-06-16\t0.32%\t\t-26.74%\n",
      "2022-08-16 ~ 2022-12-30\t4.90%\t\t-20.12%\n",
      "2022-07-27 ~ 2022-10-27\t4.26%\t\t-10.30%\n",
      "2024-07-10 ~ 2024-08-05\t-0.21%\t\t-13.12%\n",
      "2025-02-10 ~ 2025-04-07\t5.76%\t\t-20.85%\n",
      "\n",
      "累计（5个区间）黄金：15.04%  纳指：-91.14%\n"
     ]
    }
   ],
   "source": [
    "import akshare as ak\n",
    "import pandas as pd\n",
    "\n",
    "# 时间区间列表（已按要求更新）\n",
    "time_ranges = [\n",
    "    (\"2022-04-04\", \"2022-06-16\"),\n",
    "    (\"2022-08-16\", \"2022-12-30\"),\n",
    "    (\"2022-07-27\", \"2022-10-27\"),\n",
    "    (\"2024-07-10\", \"2024-08-05\"),\n",
    "    (\"2025-02-10\", \"2025-04-07\")\n",
    "]\n",
    "\n",
    "# 获取黄金主力合约收盘\n",
    "def get_au_close(start_date, end_date):\n",
    "    df = ak.futures_main_sina(\n",
    "        symbol=\"AU0\",\n",
    "        start_date=start_date.replace(\"-\", \"\"),\n",
    "        end_date=end_date.replace(\"-\", \"\")\n",
    "    )\n",
    "    df[\"日期\"] = pd.to_datetime(df[\"日期\"])\n",
    "    return df.sort_values(\"日期\").reset_index(drop=True).rename(columns={\"收盘价\": \"close\"})\n",
    "\n",
    "# 获取纳斯达克综合指数收盘\n",
    "def get_nasdaq_close(start_date, end_date):\n",
    "    nasdaq_index = ak.index_us_stock_sina(symbol=\".ixic\")\n",
    "    nasdaq_index.reset_index(inplace=True)\n",
    "    nasdaq_index['date'] = pd.to_datetime(nasdaq_index['date'])\n",
    "    mask = (nasdaq_index['date'] >= start_date) & (nasdaq_index['date'] <= end_date)\n",
    "    nasdaq_index = nasdaq_index.loc[mask, ['date', 'close']]\n",
    "    nasdaq_index.set_index('date', inplace=True)\n",
    "    return nasdaq_index.reset_index().sort_values(\"date\").reset_index(drop=True)\n",
    "\n",
    "print(\"时间区间\\t\\t黄金涨幅\\t纳斯达克涨幅\")\n",
    "\n",
    "total_gold = 0\n",
    "total_ndq  = 0\n",
    "valid = 0\n",
    "\n",
    "for s, e in time_ranges:\n",
    "    df_gold = get_au_close(s, e)\n",
    "    df_ndq  = get_nasdaq_close(s, e)\n",
    "\n",
    "    if len(df_gold) < 2 or len(df_ndq) < 2:\n",
    "        print(f\"{s} ~ {e}\\t数据不足\")\n",
    "        continue\n",
    "\n",
    "    gold_pct = (df_gold[\"close\"].iloc[-1] - df_gold[\"close\"].iloc[0]) / df_gold[\"close\"].iloc[0] * 100\n",
    "    ndq_pct  = (df_ndq[\"close\"].iloc[-1] - df_ndq[\"close\"].iloc[0]) / df_ndq[\"close\"].iloc[0] * 100\n",
    "\n",
    "    print(f\"{s} ~ {e}\\t{gold_pct:.2f}%\\t\\t{ndq_pct:.2f}%\")\n",
    "    total_gold += gold_pct\n",
    "    total_ndq  += ndq_pct\n",
    "    valid += 1\n",
    "\n",
    "if valid:\n",
    "    print(f\"\\n累计（{valid}个区间）黄金：{total_gold:.2f}%  纳指：{total_ndq:.2f}%\")\n",
    "else:\n",
    "    print(\"无有效区间数据\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
